package light.head.controller.el;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import light.head.constant.Msg;
import light.head.constant.Parm;
import light.head.constant.Url;
import light.head.dao.el.LecturerDAO;
import light.head.dao.sys.OrganDAO;
import light.head.dao.sys.UserInfoDAO;
import light.head.enm.RESULT;
import light.head.framework.base.SupporterController;
import light.head.framework.entity.PageBean;
import light.head.model.el.Lecturer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping(value = Url.C_LECTURER)
public class LecturerController extends SupporterController{
	private LecturerDAO leDAO;
	private OrganDAO organDAO;
	private UserInfoDAO userDAO;

	@RequestMapping(Url.INIT)
	public ModelAndView init(HttpServletRequest request, HttpServletResponse response){
		return new ModelAndView(Url.LECTURER_INIT);
	}
	
	@RequestMapping(Url.QUERY)
	public ModelAndView query(HttpServletRequest request, HttpServletResponse response){
		PageBean pb = createPageBean(request);
		Map<String, String> map = createQueryMapByParmName(request, Parm.LN, Parm.DELETED);
		Integer oc = organDAO.getRoot(cu_organCode(request)).getCode();
		if (leDAO.findByMap(oc, map, pb) == RESULT.SUCCESS){
			/*Course c =null;
			for (Object o : pb.getResultList()) {
				c = (Course) o;
				List<FrontCourse> list = fcDAO.findCc(c.getCode());
				c.setItemList(list);
			}*/
		}
		request.setAttribute(Parm.PAGE_BEAN, pb);
		return new ModelAndView(Url.LECTURER_LIST);
	}
	
	@RequestMapping(Url.EDIT)
	public ModelAndView edit(HttpServletRequest request, HttpServletResponse response){
		String code = request.getParameter(Parm.CODE);
		if(isNotNull(code)){
			Lecturer le = leDAO.get(Integer.parseInt(code));
			/*List<FrontCourse> list = fcDAO.findCc(course.getCode());
			course.setItemList(list);*/
			request.setAttribute(Parm.LECTURER, le);
		}
		return new ModelAndView(Url.LECTURER_EDIT);
	}
	
	@RequestMapping(Url.SAVE)
	@Transactional(rollbackFor = Exception.class)
	public void save(Lecturer le, HttpServletRequest request, HttpServletResponse response){
		RESULT result = RESULT.ERROR;
		Integer oc = organDAO.getRoot(cu_organCode(request)).getCode();
		List<Lecturer> clist = leDAO.findLc(oc,le.getCode());
		if (clist.size() > 0 && clist != null){
			for (Lecturer l :clist) {
				if(l.getCode() == le.getCode()){
					l.setLn(le.getLn());
					l.setMajor(le.getMajor());
					result = leDAO.upd(l);
				}
			}
		}else{
			le.setCt(new Date());
			le.setOc(oc);
			le.setCu(cu_code(request));
			le.setDeleted(false);
			result = leDAO.add(le);
		}
		if(RESULT.SUCCESS == result){
			leDAO.loadAll(); //重新加载缓存
			print(response, Msg.SUCCESS);
		}else{
			print(response, Msg.ERROR);	
		}
	}
	
	
	@RequestMapping(Url.SHIELD)
	public void shield(HttpServletRequest request, HttpServletResponse response){
		String code = request.getParameter(Parm.CODE);
		RESULT result = RESULT.ERROR;
		if(isNotNull(code)){
			Lecturer le = leDAO.get(Integer.parseInt(code));
			if(le != null){
				le.setDeleted(true);
				result = leDAO.upd(le);
			}
		}
		if(result == RESULT.SUCCESS){
			leDAO.loadAll(); //重新加载缓存
			print(response, Msg.SUCCESS);
		}else{
			print(response, Msg.ERROR);
		}
	}
	
	@RequestMapping(Url.UN_SHIELD)
	public void unshield(HttpServletRequest request, HttpServletResponse response){
		String code = request.getParameter(Parm.CODE);
		RESULT result = RESULT.ERROR;
		if(isNotNull(code)){
			Lecturer le = leDAO.get(Integer.parseInt(code));
			if(le != null){
				le.setDeleted(false);
				result = leDAO.upd(le);
			}
		}
		if(result == RESULT.SUCCESS){
			leDAO.loadAll(); //重新加载缓存
			print(response, Msg.SUCCESS);
		}else{
			print(response, Msg.ERROR);
		}
	}
	//选择课程
	@RequestMapping(Url.AID_INIT)
	public ModelAndView aidInit(HttpServletRequest request, HttpServletResponse response){
		return new ModelAndView(Url.LECTURER_ADDINIT);
	}
	
	@RequestMapping(Url.AID_LIST)
	public ModelAndView aidList(HttpServletRequest request, HttpServletResponse response){
		PageBean pb = createPageBean(request);
		Integer oc =organDAO.getRoot(cu_organCode(request)).getCode();
		Map<String, String> map = createQueryMapByParmName(request, Parm.REAL_NAME,Parm.ON);
		userDAO.findByMapOc(organDAO.getSonCodeList(oc), map, pb);
		request.setAttribute(Parm.PAGE_BEAN, pb);
		return new ModelAndView(Url.LECTURER_ADDLIST);
	}
	
	
	@Autowired
	public void setLeDAO(LecturerDAO leDAO) {
		this.leDAO = leDAO;
	}
	@Autowired
	public void setOrganDAO(OrganDAO organDAO) {
		this.organDAO = organDAO;
	}
	@Autowired
	public void setUserDAO(UserInfoDAO userDAO) {
		this.userDAO = userDAO;
	}
	
}
